package com.axt.manager.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.json.annotations.JSON;

import com.axt.manager.bean.NodeBean;
import com.axt.manager.model.Node;
import com.axt.soa.action.BaseAction;
import com.axt.soa.msg.types.MsgParam;
import com.axt.util.TypeConvert;
import com.opensymphony.xwork2.ActionContext;

public class NodeAction extends BaseAction{
	private NodeBean nodeBean = new NodeBean();
	private List<Node> nodes = null;
	
	private String nodeId;
	private String nodeName;
	private String nodeType;
	private String nodeAddr;
	private String port;
	private int userId;
	
	@JSON(name="nodes")
	public List<Node> getNodes() {
		return nodes;
	}

	public void setNodes(List<Node> nodes) {
		this.nodes = nodes;
	}
	
	@JSON(name="errcode")
	public int getErrcode() {
		return errcode;
	}
	
	@JSON(name="errinfo")
	public String getErrinfo() {
		return errinfo;
	}
	
	@JSON(name="nodeid")
    public String getNodeId() {
		return nodeId;
	}

	public String getNodeName() {
		return nodeName;
	}

	public void setNodeName(String nodeName) {
		this.nodeName = nodeName;
	}

	public String getNodeType() {
		return nodeType;
	}

	public void setNodeType(String nodeType) {
		this.nodeType = nodeType;
	}

	public String getNodeAddr() {
		return nodeAddr;
	}

	public void setNodeAddr(String nodeAddr) {
		this.nodeAddr = nodeAddr;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}
	
	public int getUserId() {
		return userId;
	}

	public void setUserId(int userId) {
		this.userId = userId;
	}

	//添加节点
	public String addNode() throws Exception {
    	
		setNodeId(TypeConvert.toString(nodeBean.addNode(nodeName, nodeType, nodeAddr, port)));
    	setErrcode(nodeBean.getErrcode());
		setErrinfo(nodeBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	//删除节点
	public String deleteNode() throws Exception {
    	
		nodeBean.deleteNode(nodeId);
    	setErrcode(nodeBean.getErrcode());
		setErrinfo(nodeBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	// 修改节点
	public String modifyNode() throws Exception {
    	
		nodeBean.modifyNode(nodeId, nodeName, nodeType, nodeAddr, port);
    	setErrcode(nodeBean.getErrcode());
		setErrinfo(nodeBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	// 获取NodeList
	public String queryNodeList() throws Exception {
    	if(userId == 0)
    		userId = 1;	//userId设置成管理员，获取全部节点
		setNodes(nodeBean.queryNodeList(userId));
    	setErrcode(nodeBean.getErrcode());
		setErrinfo(nodeBean.getErrinfo());
		
    	return MsgParam.SUCCESS;
    }
	
	// 获取NodeId,输入节点ip和节点端口
	public String queryNodeId() throws Exception {
		
    	// 获取NodeId
    	int serverId = nodeBean.queryServerId(nodeAddr, "9002");
    	if (serverId == -1) {
    		setErrcode(nodeBean.getErrcode());
    		setErrinfo(nodeBean.getErrinfo());
    		return MsgParam.FAILURE;
    	}
    	
    	// 获取成功
    	ActionContext actionContext = ActionContext.getContext();
    	Map<String, Object> session = actionContext.getSession();

    	session.put(MsgParam.PARA_SVR_PATH, serverId);

    	setErrcode(nodeBean.getErrcode());
		setErrinfo(nodeBean.getErrinfo());
    	return MsgParam.SUCCESS;
    }
}
